package com.thoughtworks.yuyijq.generate.element;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * User: yuyijq
 * Date: Oct 10, 2010
 * Time: 10:15:13 AM
 */
public class MethodElement implements MemberElement {
    public MethodBodyElement getMethodBodyElement() {
        return methodBodyElement;
    }

    public MethodSignatureElement getSignatureElement() {
        return signatureElement;
    }

    private MethodSignatureElement signatureElement;
    private MethodBodyElement methodBodyElement;

    public MethodElement() {
        signatureElement = new MethodSignatureElement();
        methodBodyElement = new MethodBodyElement();
    }

    public int read(String source) {
        int signatureLength = signatureElement.read(source);
        source = source.substring(signatureLength);
        int bodyLength = methodBodyElement.read(source);
        return signatureLength + bodyLength;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this).
                append("methodBodyElement", methodBodyElement).
                append("signatureElement", signatureElement).
                toString();
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        MethodElement that = (MethodElement) o;

        return new EqualsBuilder()
                .append(this.methodBodyElement, that.methodBodyElement)
                .append(this.signatureElement, that.signatureElement)
                .isEquals();
    }

    public int hashCode() {
        return new HashCodeBuilder()
                .append(this.methodBodyElement)
                .append(this.signatureElement)
                .toHashCode();
    }
}
